#include <bits/stdc++.h>
using namespace std;
using ll = long long;
using pii = pair<int, int>;
#define rep(i, a, b) for (int i = (a); i <= (b); i++)
#define per(i, a, b) for (int i = (a); i >= (b); i--)
#define endl '\n'
const int N = 3e6 + 5;

int n, k;
int a[N];

int main() {
  ios::sync_with_stdio(0), cin.tie(0), cout.tie(0);
  cin >> k >> n;
  rep(i, 1, n) cin >> a[i];
  deque<int> mn, mx;
  int pre = 0, ans = 0;
  rep(i, 1, n) {
    while (mx.size() && a[mx.front()] - k > a[i]) {
      pre = max(pre, mx.front());
      mx.pop_front();
    }
    while (mn.size() && a[mn.front()] < a[i] - k) {
      pre = max(pre, mn.front());
      mn.pop_front();
    }
    ans = max(ans, i - pre);
    while (mx.size() && a[mx.back()] <= a[i]) mx.pop_back();
    while (mn.size() && a[mn.back()] >= a[i]) mn.pop_back();
    mx.push_back(i);
    mn.push_back(i);
  }
  cout << ans;
  return 0;
}